#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

void solve() {
	int n; cin >> n;
	ll t1 = 2e9, t2 = 2e9, t3 = 2e9;
	int f1 = 0, f2 = 0, f3 = 0;
	for (int i = 0; i < n; i++) {
		ll x;
		string s;
		cin >> x >> s;
		if (s == "11")        f3 = 1, t3 = min(t3, x);
		else if (s[0] == '1') f1 = 1, t1 = min(t1, x);
		else if (s[1] == '1') f2 = 1, t2 = min(t2, x);
	}
	if ((f1 && f2) || f3)cout << min(t1 + t2, t3) << endl;
	else cout << -1 << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}